package com.xpgk.mvc.bsendMessage;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jodd.util.RandomString;
import jodd.util.StringUtil;

public class StringUtils {
	public static final String RAND_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	public static final String BLANK_STR = "";
	public static final String NULL_STR = "NULL";
	
	public static final String RAND_NUM_STR = "1234567890";
	public static final char[] RAND_CHAR_ARR = RAND_STR.toCharArray();
	public static final char[] RAND_NUM_CHAR_ARR = RAND_NUM_STR.toCharArray();
	
	public static String randStr(int len){
		return RandomString.getInstance().random(len, RAND_STR);
	}
	
	public static String concat(String...strs){
		StringBuilder sb = new StringBuilder();
		for (String str : strs) 
		{
			sb.append(str);
		}
		return sb.toString();
	}
	
	
	public static String uniqueStrWithDateTime(){
		StringBuilder sb= new StringBuilder();
		Random rendom = new Random();
		String conector = "_";
		String timeDesc = DateUtil.dateFormat("yyyyMMddHHmmss",new Date());
		sb.append(timeDesc)
			.append(conector)
			.append(rendom.nextInt(10000))
			.append(conector)
			.append(rendom.nextInt(10000))
			.append(conector)
			.append(rendom.nextInt(10000));
		return sb.toString();
	}
	
	
	
	public static String rand(int len){
		return RandomString.getInstance().random(len, RAND_CHAR_ARR);
	}
	
	public static String randNum(int len){
		return RandomString.getInstance().random(len, RAND_NUM_CHAR_ARR);
	}
	
	/** 
     * 手机号验证 
     *  
     * @param  str 
     * @return 验证通过返回true 
     */  
    public static boolean isMobile(String str) {   
        Pattern p = null;  
        Matcher m = null;  
        boolean b = false;   
        p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号  
        m = p.matcher(str);  
        b = m.matches();   
        return b;  
    } 
	
    public static String substring(String text,int beginIndex,int length){
    	int n=text.length();
    	n=Math.min(beginIndex+length, n-beginIndex);
    	return text.substring(beginIndex,n);
    }
    
    public static String ToString(Object obj)
	{
		if(obj==null)
			return null;
		else {
			return obj.toString();
		}
	}
    
    /**
	 * 字符串连接操作
	 * @param list
	 * @param delimiter
	 * @return
	 */
    public static String join(List<?> list, String delimiter){
    	StringBuilder sb = new StringBuilder();
    	if(list == null || list.isEmpty()){
    		return sb.toString();
    	}
    	Iterator<?> iter =list.iterator();
    	while(iter.hasNext()){
    		sb.append(StringUtil.toSafeString(iter.next()))
    		  .append(StringUtil.toSafeString(delimiter));
    	}
    	return sb.substring(0, sb.length() - StringUtil.toSafeString(delimiter).length());
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
